package com.jxwifi.cloud.quickclean.main;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.idroid.utils.NoDoubleClickListener;
import com.jxwifi.cloud.quickclean.app.AccountMgr;
import com.jxwifi.cloud.quickclean.app.LocationGaoDe;
import com.jxwifi.cloud.quickclean.app.StateUtils;
import com.jxwifi.cloud.quickclean.app.UrlPool;
import com.jxwifi.cloud.quickclean.bean.HomeaddressBean;
import com.jxwifi.cloud.quickclean.bean.InfoBean;
import com.jxwifi.cloud.quickclean.model.AuzImpl;
import com.jxwifi.cloud.quickclean.okhttp.Params;
import com.jxwifi.cloud.quickclean.utils.KAlertDialog;
import com.jxwifi.cloud.quickclean.utils.ToastUtils;

public class EpidemicMaskPresenter {

    private Context mContext;

    private EpidemicMaskView mView;

    private AuzImpl mAuz;

    private String AddressID="";

    public EpidemicMaskPresenter(Context context,EpidemicMaskView view){
        this.mContext=context;
        this.mView=view;
        mAuz=new AuzImpl();
    }

    public void initView(){
        mView.getEmPhone().setText(AccountMgr.gettell(mContext));
        getinfo();
    }

    //用户信息
    public void getinfo() {
        Params params = new Params();
        params.add("customerCode", AccountMgr.getCode(mContext));
        mAuz.auzget(UrlPool.GET_INFO, params, new AuzImpl.AuzListener() {
            @Override
            public void AuzSuccess(int code, String data) {
                if (code == 0) {
                    InfoBean minfoBean = JSON.parseObject(data, InfoBean.class);
                    AccountMgr.putUser(mContext, minfoBean, true);
                    onAddressBean(minfoBean.getDefaultAddress());
                } else {
                    mView.oninfo();
                }
            }

            @Override
            public void AuzFailure(int errorCode, String msg) {
                mView.onFailure(errorCode, msg);
            }
        });
    }

    public void onAddressBean(HomeaddressBean address) {
        //判断地址信息是否未空，为空显示空布局
        if (address != null) {
            AddressID = address.getId();
            mView.getAddressLayout().setVisibility(View.VISIBLE);
            mView.getNullAddress().setVisibility(View.GONE);
            mView.getAddressText().setText(address.getProvince() + " " + address.getCity() + " " + address.getArea());
            StateUtils.typeAddress(mContext, mView.getAddressText(), address.getTypeCode());
            mView.getAddressDetails().setText(address.getAddress());
        } else {
            AddressID = "";
            mView.getNullAddress().setVisibility(View.VISIBLE);
            mView.getAddressLayout().setVisibility(View.GONE);
        }
    }

    //接口请求前条件判断
    public void onJudgment(){
        if (TextUtils.isEmpty(AddressID)){
            Toast toast = Toast.makeText(mContext, "详细地址不能为空", Toast.LENGTH_SHORT);
            ToastUtils.showMyToast(toast, 3000);
            return;
        }
        if (TextUtils.isEmpty(mView.getEmPhoneName().getText().toString())){
            Toast toast = Toast.makeText(mContext, "联系人姓名不能为空", Toast.LENGTH_SHORT);
            ToastUtils.showMyToast(toast, 3000);
            return;
        }
        final KAlertDialog dialogshow=new KAlertDialog(mContext,"收到申请后会于2-3个工作日寄出（到付）");
        dialogshow.setRightTxt("确定", new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {

                onReceive();
                dialogshow.dismiss();
            }
        });
        dialogshow.show();
    }

    //领取接口请求
    public void onReceive(){
        Params params=new Params();
        params.add("addressId",AddressID);
        params.add("userName",mView.getEmPhoneName().getText().toString());
        params.add("userTell",mView.getEmPhone().getText().toString());
        mAuz.auzpost(UrlPool.POST_RECEIVE_MASK, params, new AuzImpl.AuzListener() {
            @Override
            public void AuzSuccess(int code, String data) {
                if (code==0){
                    mView.ReceiveSuccess("您已申请领取成功");
                }
            }

            @Override
            public void AuzFailure(int errorCode, String msg) {
                mView.onFailure(errorCode, msg);
            }
        });
    }
}
